package com.util;

import com.servlet.RightServlet;

import java.sql.*;

public class UtilJdbc {
    private static final String driver="com.mysql.cj.jdbc.Driver";
    private static final String url="jdbc:mysql://localhost:3306/us";
    private static final String mysqlname="root";
    private static final String mysqlpassword="123456";

    /**
     * 登录
     * @param
     * @return
     */
    public ResultSet loginDbManager(String str){
        System.out.println("dbManager-SQL:"+str);
        ResultSet resultSet = null;
        try {
            Class.forName(driver);//注册连接数据库对象//java程序连接数据库的驱动包注册
            Connection conn= DriverManager.getConnection(url,mysqlname,mysqlpassword);//数据库信息
            Statement stmt =conn.createStatement();    //创建 执行sql语句对象
            resultSet = stmt.executeQuery(str); //发送执行sql语句
            System.out.println("SQL 执行结果 " +resultSet);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }
    public ResultSet login(String uname,String upassword){
        ResultSet set=null;
        String loginSql="select *from user where user_name='"+uname+"' and user_password='"+upassword+"'";
        set=loginDbManager(loginSql);
        return set;
    }
    /**
     * 注册
     * @param
     * @return
     */
    public static boolean searchDbManager(String str){
        System.out.println("sdbManager-SQL:"+str);
        boolean bo=false;
        try {
            Class.forName(driver);//注册连接数据库对象//java程序连接数据库的驱动包注册
            Connection conn= DriverManager.getConnection(url,mysqlname,mysqlpassword);//数据库信息
            Statement stmt =conn.createStatement();    //创建 执行sql语句对象
            stmt.execute(str); //发送执行sql语句
            bo=true;
            System.out.println("SQL 执行结果 " +bo);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return bo;
    }



    public boolean search(String uname,String upassword,String Email){
        boolean b=false;
        String searchSql="INSERT INTO user(user_name,user_password,email) VALUES ('"+uname+"','"+upassword+"','"+Email+"');";
        b=searchDbManager(searchSql);
        return b;
    }
/*    public boolean search(String zuname,String zupassword,String Email){

        String str="insert into user(user_name,user_password,email) values ('"+zuname+"','"+zupassword+"','"+Email+"');";
        boolean bo=false;
        try {
            Class.forName(driver);//注册连接数据库对象//java程序连接数据库的驱动包注册
            Connection conn= DriverManager.getConnection(url,mysqlname,mysqlpassword);//数据库信息
            Statement stmt =conn.createStatement();    //创建 执行sql语句对象
            bo=stmt.execute(str); //发送执行sql语句
            bo=true;
            System.out.println("SQL 执行结果 " +bo);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }         return bo;
    }*/

    /**
     *主页显示
     *
     */
    public ResultSet frist(){
        ResultSet set=null;
        String loginSql="select actname,planner,acttime,actplace,actid,member  from activity;";
        set=loginDbManager(loginSql);
        return set;
    }
    /**
     * 详情页
     */
    public ResultSet second(String na){
        ResultSet set=null;
        String loginSql="select * from activity where actid='"+na+"'";
        set=loginDbManager(loginSql);
        return set;
    }
    /**
     * 创建活动
     */
    public boolean create(String actname,String planner,String member,String activityContent,String actid,String acttime,String actplace) {
        boolean b = false;
        String searchSql ="INSERT INTO activity(actname,planner,member,activityContent,actid,acttime,actplace) VALUES('"+actname+"','"+planner+"','"+member+"','"+activityContent+"','"+actid+"','"+acttime+"','"+actplace+"')";
        b = searchDbManager(searchSql);
        return b;
    }

    /**
     * 报名
     */
    public boolean join(String uname,String na){
        boolean b=false;
        String joinSql="insert into appact(aname,joinact) values('"+uname+"','"+na+"')";
        b=searchDbManager(joinSql);
        return b;
    }

    /**
     * 修改
     * @return
     */
    public static boolean updateDbManager(String str){
        System.out.println("sdbManager-SQL:"+str);
        boolean bo=false;
        try {
            Class.forName(driver);//注册连接数据库对象//java程序连接数据库的驱动包注册
            Connection conn= DriverManager.getConnection(url,mysqlname,mysqlpassword);//数据库信息
            Statement stmt =conn.createStatement();    //创建 执行sql语句对象
            stmt.executeUpdate(str); //发送执行sql语句
            bo=true;
            System.out.println("SQL 执行结果 " +bo);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return bo;
    }
    public boolean update(){
        boolean a=false;
        String updateSql="";
        a=updateDbManager(updateSql);
        return a;
    }


}
